import store from '../store'
import {getLyric, getSongDetail, getSongUrl} from "@/network/song/song";

const getSingleMusicPlay = (id) => {
    let audio = {};
    getSongDetail(id).then(res => {
        audio.name = res.data.songs[0].name;
        audio.cover = res.data.songs[0].al.picUrl;
        audio.theme = '#100f0f';
        let arts = '';
        if (res.data.songs[0].ar.length > 1) {
            res.data.songs[0].ar.forEach((e) => {
                arts += e.name;
                arts += '/';
            })
        } else {
            arts += res.data.songs[0].ar[0].name;
        }
        audio.artist = arts;
    })
    getSongUrl(id).then(res => {
        audio.url = res.data.data[0].url;
    })
    getLyric(id).then(res => {
        audio.lrc = res.data.lrc.lyric;
    })
// 清除播放器状态
    store.state.audio = [];
    const audioArr = new Array(audio);
    store.commit('setAudio', audioArr);
}
// 列表播放
const getSingleMusicList = (tracks) => {
    let len = tracks.length;
    let tmpObject = tracks;
    let audioList = [];
    for (let i = 0; i < len; i++) {
        let audio = {};
        getSongDetail(tmpObject[i].id).then(res => {
            audio.name = res.data.songs[0].name;
            audio.cover = res.data.songs[0].al.picUrl;
            audio.theme = '#100f0f';
            let arts = '';
            if (res.data.songs[0].ar.length > 1) {
                res.data.songs[0].ar.forEach((e) => {
                    arts += e.name;
                    arts += '/';
                })
            } else {
                arts += res.data.songs[0].ar[0].name;
            }
            audio.artist = arts;
        })
        getSongUrl(tmpObject[i].id).then(res => {
            audio.url = res.data.data[0].url;
        })
        getLyric(tmpObject[i].id).then(res => {
            if (typeof (res.data.lrc) === 'undefined') {
                return null;
            } else {
                audio.lrc = res.data.lrc.lyric;
            }
        })
        audioList[i] = audio;
    }
// 清除播放器状态
    store.state.audio = [];
    store.commit('setAudio', audioList);
}
export {
    getSingleMusicPlay,
    getSingleMusicList
}